import React, { Component } from 'react'
import { useDispatch } from 'react-redux';
import { asyncAddTodo } from '../../store/slice/todos';
import './index.css'
export default function Header() {
    const dispatch = useDispatch()
    function keyUpHandler(e){
        // 0. 判断是不是回车
        // 1. 获取输入内容
        // 2. 组合一个todo
        // 3. 发送请求添加todo
        // 4. 清空文本框
        if(e.code !== 'Enter') return;
        let todo = {
            title:e.target.value.trim(),
            isDone:false
        }
        dispatch(asyncAddTodo(todo))
        e.target.value = ''
    }
    return (
        <div className="todo-header">
            <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={keyUpHandler}/>
        </div>
    )
}
